#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
int n;

char r[5][15];
struct fo{
	char ip[26];
	int no;
};
fo os[1005];
int main(){
	FILE *fin,*fout;
	fin=fopen("network.in","rb");
	fout=fopen("network.out","wb");
	//fin=stdin,fout=stdout;
	fscanf(fin,"%d",&n);
	for(int i=1;i<=n;i++){
		char op[6],ad[22];
		fscanf(fin,"%s %s",op,ad);
		int dn=0,mh=0;
		bool ok=true;
		int osn=0;
		if(strlen(ad)>25||strlen(ad)<8){
			fprintf(fout,"ERR\n");
			continue;
		}
		int hw=0;
		for(int j=0;j<strlen(ad);j++){
			if(ad[j]=='.'){
				dn++;
				if(ad[j+1]=='0'){
					ok=false;
					break;
				}
				hw=0;
			}
			if(ad[j]==':'){
				if(dn==3){
					mh++;
					if(ad[j+1]=='0'){
						ok=false;
						break;
					}
					hw=0;
				}else{
					ok=false;
					break;
				}
			}
			if(dn>3||mh>1){
				ok=false;
				break;
			}else{
				r[dn+mh][hw++]=ad[j];
			}
		}
		if(!ok){
			fprintf(fout,"ERR\n");
			continue;
		}
		for(int j=0;j<5;j++){
			int rd=0,rmd=0;
			for(int k=strlen(r[i]);k>=0;k--){
				rd+=pow(10,rmd++)*r[j][k];
			}
			if((rd>=0&&rd<=255&&i<=3)||(rd>=0&&rd<=65536&&i==4)){
				if(op[0]=='S'){
					for(int sld=0;sld<500;sld++){
						if(strcmp(ad,os[sld].ip)){
							fprintf(fout,"FAIL\n");
							break;
						}
						fprintf(fout,"OK\n");
						os[osn++].no=i;
					}
				}else{
					for(int sld=0;sld<500;sld++){
						if(strcmp(ad,os[sld].ip)){
							fprintf(fout,"%d\n",os[sld].no);
							break;
						}
						fprintf(fout,"FAIL\n");
						
					}
				}
			}else{
				fprintf(fout,"ERR\n");
				break;
			}
		}
		/*if(ok){
			
		}else{
			fprintf(fout,"ERR\n");
		}*/
	}
	fclose(fin);
	fclose(fout);
	return 0;
} 
